
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.labels = {};
        this.nodes = {};

        this._registerAllBtnClickEvent(this.node);

    },

    init() {

    },
    // start() {

    // },

    /**
     * 注册所有按钮的点击事件
     * @param node
     */
    _registerAllBtnClickEvent: function (node) {
        if (!node) {
            return;
        }
        let name = node.name;
        this.nodes[name] = node;
        let button = node.getComponent("cc.Button");
        let label = node.getComponent("cc.Label");
        if (label) {
            if (this.labels[name]) {
                cc.warn("存在同名的Label节点:" + name);
            }
            this.labels[name] = label;
        }
        if (button && name != "Handle") {

            // if (this.buttons[name]) {
            //     // cc.warn("存在同名的Button节点:" + name);
            // }
            // this.buttons[name] = button;
            // button = node.getComponent("cc.Button");
            // cc.log("监听点击事件:" + name);
            node.on('click', () => {
                let time = new Date().getTime();
                if (this.pressTime && (time - this.pressTime) <= 300) {
                    // cc.log("过滤过快的点击间隔ms:" + 300);
                    // return;
                }
                this.pressTime = time;
                this._onBtnClickCallback(node);
            }, this);
        }
        let arrayRootChildren = node.children;
        let length = arrayRootChildren.length;
        for (let i = 0; i < length; i++) {
            let child = arrayRootChildren[i];
            this._registerAllBtnClickEvent(child);
        }
    },
    _onBtnClickCallback: function (sender) {
        cc.log("按钮被点击了:" + sender.name);
        let audioSource = sender.getComponent("cc.AudioSource");
        if (audioSource && audioSource.clip) {
            cc.log("播放被点击的按钮的AudioSource");
            //  audioSource.play();
            cc.audioEngine.playEffect(audioSource.clip, false);
            // cc.audioEngine.playMusic(audioSource.clip, false);
        } else {
            // GlobalRes.playEffect("Common/Sound/UI_dianjikai01");
        }
        if (this._onClick) {
            this._onClick(sender);
        }
    },
    // update (dt) {},
});